package day03;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.jar.JarOutputStream;

public class MapDemo {
    public static void main(String[] args) {
        Map<String,Double> map = new HashMap<>();
        map.put("张a",100.0);
        map.put("张b",101.0);
        map.put("张c",102.0);
        map.put("张d",1002.0);
        System.out.println(map);
        System.out.println("-----------1-------------");

        map.remove("张a");
        System.out.println(map);
        System.out.println("-----------2-------------");

        Boolean b = map.containsKey("张b");
        System.out.println(b);
        Boolean c = map.containsValue(101.0);
        System.out.println(c);
        System.out.println("-----------3-------------");

        Boolean d = map.isEmpty();
        System.out.println(d);
        int e = map.size();
        System.out.println(e);
        System.out.println("----------4-------------");

        test1();
        System.out.println("----------5-------------");

        test2();
        System.out.println("----------6-------------");

        test3();


    }
    public static void test1(){
        Map<String,Double> map = new HashMap<>();
        map.put("张a",100.0);
        map.put("张b",101.0);
        map.put("张c",102.0);
        map.put("张d",1002.0);
        //将map中的所有键值取出来放到set集合中
        Set<String> keySet = map.keySet();
        //遍历key集合
        for(String key:keySet){
            //根据key值去找值
            Double value = map.get(key);
            System.out.println(key+":"+value);
        }
    }
    public static void test2(){
        Map<String,Double> map = new HashMap<>();
        map.put("张a",100.0);
        map.put("张b",101.0);
        map.put("张c",102.0);
        map.put("张d",1002.0);
        //1.将map集合转成set集合（将map中的key-value绑定成一个对象entry)
        Set<Map.Entry<String,Double>> entrySet = map.entrySet();
        //2.遍历set集合
        for(Map.Entry<String,Double> entry:entrySet){
            //3.根据entry获取key和value
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key+":"+value);
        }
    }
    public static void test3(){
        Map<String,Double> map = new HashMap<>();
        map.put("张a",100.0);
        map.put("张b",101.0);
        map.put("张c",102.0);
        map.put("张d",1002.0);

        map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String key, Double value) {
                System.out.println(key + ":" + value);
            }
        });
        System.out.println("----------6-------------");
        map.forEach((k,v)->{System.out.println(k+":"+v);});
        System.out.println("----------7-------------");

    }
}
